<template>
  <div class="UserIndexDialog">
    <el-dialog
      :close-on-click-modal="false"
      title="数据导入"
      :visible="dialog"
      width="300px"
      :before-close="closeFn"
    >
      <span>
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          multiple
          :limit="1"
          :on-change="onChange"
          :auto-upload="false"
          :file-list="excelFile">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">支持扩展名：xls、xlsx，文件不得大于1M</div>
        </el-upload>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn" size="medium">取消</el-button>
        <el-button type="primary" size="medium" @click="uploadFile">确定</el-button>
      </span>
    </el-dialog>
    <!-- 弹出框结束 -->
  </div>
</template>

<script>
import { uploadGoods } from '@/api/skuAPIs'
export default {
  props: {
    dialog: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      excelFile: ''
    }
  },
  methods: {
    async closeFn () {
      // 重置状态
      this.$emit('update:dialog', false)
    },
    onChange (file) {
      this.excelFile = file.raw
    },
    async uploadFile () {
      if (!this.excelFile) {
        this.$message.error('您未选择文件')
        return
      }
      try {
        const formData = new FormData()
        formData.append('fileName', this.excelFile)
        console.log(formData)
        await uploadGoods(formData)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
